// subPages/health/recordSugar/record.js
const app = getApp()
var util =  require('../../../utils/util.js');
var dateTimePicker = require('../../../utils/dateTimePicker.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ["请选择测量点","早餐前","早餐后","午餐前","午餐后","晚餐前","晚餐后"],
    index:0,
    downImg:'/resources/glucometer/down.png',
    upImg: '/resources/glucometer/up.png',
    bloodData: '0.0',
    symbol: '<',
    pBtnTxt:{
      pBtnTxt: '保存'
    },
    dateTimeArray: null,
    dateTime: null,
    startYear: 2000,
    endYear: 2050,
    isInput: false,
    readText:'读取'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      timePointImgsrc:this.data.downImg,
      date: util.setTime11(new Date())
    })
    this.getCont()
  },

  //获得数
  getCont:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/health/info.py',
      data: {
        type: 1,
        userId: app.globalData.userId,
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        that.setData({
          readCount: res.data
        })
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //改变input
  inputBtn:function(){
    this.setData({
      isInput: true,
      readText: '完成'
    })
  },

  readInput:function(e){
    this.setData({
      bloodData : e.detail.value
    })
    console.log(this.data.bloodData)
  },

  //picker
  bindTimePointChange: function (e) {
    this.setData({
      index: e.detail.value,
      timePoint: this.data.array[e.detail.value],
      timePointImgsrc: this.data.downImg,
    })
  },
  
  bindTimePointCancel:function(){
    this.setData({
      timePointImgsrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e)
  {
    var that = this
    if(that.data.timePointImgsrc == that.data.downImg)
    {
      that.setData({
        timePointImgsrc: that.data.upImg
      })
    }
  },

  //读取按钮
  readBtn:function(e){
    var that = this
    var text = e.currentTarget.dataset.text
    if(text == '读取'){
      if(that.data.readCount == 0) {
        util.showModal('提示','当前用户没有可读取的数据')
        return
      }
      wx.request({
        url: app.globalData.apiUrl +'/health/info.py',
        data: {
          type: 2,
          userId: app.globalData.userId
        },
        method: 'get',
        header: {
          'content-type': 'application/json' //默认值
        },
        success: function (res) {
          console.log(res)
          if(res.data.code == '200'){
            
          }else{
            util.showModal('提示','当前用户没有测量过,请测完重试')
            return
          }
        },
        fail: function (res) {
          console.log("失败",res);
        }
      })
    }else if(text == '完成'){
      let time =  '2018-12-10 10:10'
      that.setData({
        dateTime: [18,11,9,10,10],
        isInput: false,
        readText: '读取'
      })
      // console.log(that.data.dateTime)
    }

    
  },

  //保存
  lickBtn:function(){
    var that = this
    if(that.data.timePoint == '' || that.data.timePoint == '请选择测量点')
    {
      util.showModal('提示','请选择测量点')
      return;
    }
    let type =  that.typeConversion(that.data.timePoint);
    if(type == 0){
      util.showModal('提示','请选择测量点')
      return;
    }
    wx.request({
      url: app.globalData.apiUrl +'/sugar',
      data: {
        userId: app.globalData.userId,
        type: type
      },
      method: 'put',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          that.setData({
            bloodData: res.data.data
          })
        }else{
          util.showModal('提示','当前用户没有测量过,请测完重试')
          return;
        }
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //测量时间点类型转换
  typeConversion:function(type){
    if(type == '早餐前'){
      return 1
    }
    if(type == '早餐后'){
      return 2
    }
    if(type == '午餐前'){
      return 3
    }
    if(type == '午餐后'){
      return 4
    }
    if(type == '晚餐前'){
      return 5
    }
    if(type == '晚餐后'){
      return 6
    }else{
      return 0 
    }
  },

  //选择时间方法
  changeDateTime(e) {
    this.setData({
      dateTime: e.detail.value
    });
  },

  changeDateTimeColumn(e) {
    var arr = this.data.dateTime,
      dateArr = this.data.dateTimeArray;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})